﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;

namespace VentaPorCatalogos.UserControls
{
    public enum BotonesTipos
    {
        Buscar = 0,
        Nuevo,
        Editar,
        Eliminar,
        Aceptar,
        Cancelar
    }

    public class BotoneraEventArgs : EventArgs
    {
        public BotonesTipos Boton { get; set; }
        public bool CancelarOperacion { get; set; }
    }

    public partial class Botonera : System.Web.UI.UserControl
    {
        public delegate void BotonClickDelegate(object sender, BotoneraEventArgs e);
        [Browsable(true)]
        [EditorBrowsable(EditorBrowsableState.Advanced)]
        public event BotonClickDelegate BotonClick;

        protected enum behavior { Initial, Edit, New, View };

        private bool _Enabled = true;
        public bool Enabled
        {
            get
            { 
                return _Enabled; 
            }
            set
            {
                _Enabled = value;

                // Si se deshabilita, se deshabilita todo
                if (!_Enabled)
                {
                    btnBuscar.Enabled = false;
                    btnNuevo.Enabled = false;
                    btnEditar.Enabled = false;
                    btnEliminar.Enabled = false;
                    btnAceptar.Enabled = false;
                    btnCancelar.Enabled = false;
                }
                else
                {   // Si se habilita, se vuelve al estado en el que estaba la botonera.
                    switch ((behavior)ViewState["EstadoBotonera"])
                    {
                        case behavior.Edit:
                            OperacionesEdicion();
                            break;
                        case behavior.Initial:
                            Inicio();
                            break;
                        case behavior.New:
                            OperacionesNuevo();
                            break;
                        case behavior.View:
                            Consulta();
                            break;                    
                    }
                }
            }
        }


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Inicio();
            }
        }

        #region "Eventos Botones"
        protected void btnBuscar_Click(object sender, EventArgs e)
        {
            if (BotonClick != null)
            {
                BotoneraEventArgs wEventArgs = new BotoneraEventArgs() { Boton = BotonesTipos.Buscar };
                BotonClick(this, wEventArgs);
                if (!wEventArgs.CancelarOperacion)
                    Consulta();
            }
        }

        protected void btnNuevo_Click(object sender, EventArgs e)
        {
            if (BotonClick != null)
            {
                //BotonClick(this, new BotoneraEventArgs() { Boton = BotonesTipos.Nuevo });
                //Operaciones();

                BotoneraEventArgs wEventArgs = new BotoneraEventArgs() { Boton = BotonesTipos.Nuevo };
                BotonClick(this, wEventArgs);
                if (!wEventArgs.CancelarOperacion)
                    OperacionesNuevo();
            }
        }

        protected void btnEditar_Click(object sender, EventArgs e)
        {
            if (BotonClick != null)
                BotonClick(this, new BotoneraEventArgs() { Boton = BotonesTipos.Editar });
            OperacionesEdicion();

        }

        protected void btnEliminar_Click(object sender, EventArgs e)
        {
            if (BotonClick != null)
                BotonClick(this, new BotoneraEventArgs() { Boton = BotonesTipos.Eliminar });
            Inicio();
        }

        protected void btnAceptar_Click(object sender, EventArgs e)
        {
            if (BotonClick != null)
            {
                BotoneraEventArgs wEventArgs = new BotoneraEventArgs() { Boton = BotonesTipos.Aceptar };
                BotonClick(this, wEventArgs);
                if (!wEventArgs.CancelarOperacion)
                    Consulta();
            }            
        }

        protected void btnCancelar_Click(object sender, EventArgs e)
        {
            if (BotonClick != null)
                BotonClick(this, new BotoneraEventArgs() { Boton = BotonesTipos.Cancelar });

            if ((behavior)ViewState["EstadoBotonera"] == behavior.Edit)
                Consulta();
            else
                Inicio();
        }

        #endregion


        #region "Métodos privados"

        protected void Inicio()
        {
            ViewState.Add("EstadoBotonera", behavior.Initial);

            btnBuscar.Enabled = true;
            btnNuevo.Enabled = true;
            btnEditar.Enabled = false;
            btnEliminar.Enabled = false;
            btnAceptar.Enabled = false;
            btnCancelar.Enabled = false;
        }

        protected void OperacionesNuevo()
        {
            ViewState.Add("EstadoBotonera", behavior.New);

            btnBuscar.Enabled = false;
            btnNuevo.Enabled = false;
            btnEditar.Enabled = false;
            btnEliminar.Enabled = false;
            btnAceptar.Enabled = true;
            btnCancelar.Enabled = true;
        }

        protected void OperacionesEdicion()
        {
            ViewState.Add("EstadoBotonera", behavior.Edit);

            btnBuscar.Enabled = false;
            btnNuevo.Enabled = false;
            btnEditar.Enabled = false;
            btnEliminar.Enabled = false;
            btnAceptar.Enabled = true;
            btnCancelar.Enabled = true;
        }

        protected void Consulta()
        {
            ViewState.Add("EstadoBotonera", behavior.View);

            btnBuscar.Enabled = true;
            btnNuevo.Enabled = true;
            btnEditar.Enabled = true;
            btnEliminar.Enabled = true;
            btnAceptar.Enabled = false;
            btnCancelar.Enabled = false;
        }

        #endregion


        #region "Métodos Públicos"


        #endregion


    }
}